import request from '@/modules/index/utils/request';
import {downloadFile} from '@/utils/common';
import {Message} from 'element-ui';

const store = {
    actions: {
        // 获取历史连接
        async dataAdminDatabaseinfoHistory(context, params) {
            const data = await request({
                url: 'databaseinfo/history',
                method: 'get',
                params: params
            });
            return data;
        },
        // 新增连接
        async dataAdminDatabaseinfoInsertDataBase(context, params) {
            const data = await request({
                url: 'databaseinfo/insert-database',
                method: 'post',
                data: params
            });
            return data;
        },
        // 删除连接
        async dataAdminDatabaseinfoDeleteDataBase(context, params) {
            const data = await request({
                url: 'databaseinfo/delete-database',
                method: 'post',
                params: params
            });
            return data;
        },
        // 修改连接密码
        async dataAdminDatabaseinfoUpdateDataBase(context, params) {
            const data = await request({
                url: 'databaseinfo/update-database',
                method: 'get',
                params: params
            });
            return data;
        },
        // 获取素材列表
        async dataAdminDatabaseinfoGetTableListId(context, params) {
            const data = await request({
                url: `databaseinfo/get-table-list/${params.id}`,
                method: 'get',
                params: params
            });
            return data;
        },
        // 根据表格获取预览前10行数据
        async dataAdminDatabaseinfoGetByTable(context, params) {
            const data = await request({
                url: 'databaseinfo/get-by-table',
                method: 'get',
                params: params
            });
            return data;
        },
        // 根据sql获取预览前10行数据
        async dataAdminDatabaseinfoGetBySql(context, params) {
            const data = await request({
                url: 'databaseinfo/get-by-sql',
                method: 'post',
                data: params
            });
            return data;
        },
        // 校验sql
        async dataAdminDatabaseinfoCheckoutIllegal(context, params) {
            const data = await request({
                url: 'databaseinfo/checkout-illegal',
                method: 'post',
                params: params
            });
            return data;
        },
        // 下载模板
        async dataAdminMaterialInfoGetUrl(context, params) {
            const data = await request({
                url: '/material-info/getUrl',
                method: 'post',
                params: params,
                responseType: 'blob',
            });
            if (!data.data) {
                Message.error("生成文件失败");
                return;
            }
            downloadFile(data);
        },
        // 下载
        async dataAdminMaterialInfoDownloadfile(context, params) {
            const data = await request({
                url: '/material-info/downloadfile',
                method: 'post',
                params: params,
                responseType: 'blob',
            });
            if (!data.data) {
                Message.error("生成文件失败");
                return;
            }
            downloadFile(data);
        },
    }
};

export default store;
